layui.define(function (exports) {
    let {$, layer, form, admin, setter, table, upload, view} = layui;
    let methods = {
        adminLayer: function (lay = {}, template = {}, request = {}) {
            let success = lay.success, requestUrl = request.url, requestDone = request.success;
            delete lay.success;
            delete request.url;
            delete request.success;
            admin.popup($.extend({
                area: ['55%', '65%'],
                btn: ['提交', '关闭'],
                btnAlign: 'c',
                maxmin: true,
                success: function(layero, index) {
                    if (template.tpl) {
                        view(this.id).render(template.tpl, {
                            mode: template.mode||'add', data: template.data||{},
                        }).then(function () {
                            //
                        }).done(function() {
                            template.id && form.render(null, template.id);
                        });
                    }
                    "function" == typeof success && success(layero, index);
                },
                yes: function(index, layero) {
                    if (template.id && requestUrl) {
                        form.submit(template.id, function(data) {
                            methods.request($.extend(request, {
                                url: requestUrl,
                                data: data.field,
                                done: function (result) {
                                    if (result.code !== 200) {
                                        return layer.msg(result.message, {icon: 5});
                                    }
                                    layer.close(index);
                                    layer.msg(result.message, {icon: 6});
                                    "function" == typeof requestDone && requestDone(data.field, result);
                                }
                            }));
                        });
                    }
                }
            }, lay));
        },

        toolOpera: function (title = '', request = {}) {
            let done = request.success;
            delete request.success;
            layer.confirm(title, {icon: 3}, function(index) {
                if (request.url) {
                    methods.request($.extend(request, {
                        done: function (result) {
                            if (result.code !== 200) {
                                return layer.msg(result.message, {icon: 5});
                            }
                            layer.close(index);
                            layer.msg(result.message, {icon: 6});
                            "function" == typeof done && done(result);
                        }
                    }));
                }
            });
        },

        request: function (op = {}) {
            if (!op.url) return false;
            let done = op.done;
            delete op.done;
            op.url = methods.apiUrl(op.url);
            let requestIndex = layer.msg('正在处理请求...', { icon: 16, shade: 0.01, time: false });
            $.ajax($.extend({
                data: {},
                async: true,
                dataType: "json",
                headers: {
                    Authorization: methods.getToken()
                },
                complete: function(XMLHttpRequest, textStatus) {
                    const Authorization = XMLHttpRequest.getResponseHeader('Authorization');
                    if (Authorization) {
                        const [ty, tk] = Authorization.split(' ');
                        AppGeneral.localStorage.setItem("TOKEN", tk, 7200);
                    }
                },
                success: function (result) {
                    layer.close(requestIndex);
                    "function" == typeof done && done(result);
                },
                error: function () {
                    layer.close(requestIndex);
                    layer.msg("网络错误", {icon: 5});
                }
            }, op));
        },

        table: function (op= {}) {
            op.url && (op.url = methods.apiUrl(op.url));
            return table.render($.extend({
                page: true,
                toolbar: false,
                defaultToolbar: false,
                headers: {
                    Authorization: methods.getToken()
                },
                response: {
                    statusCode: 200,
                    msgName: 'message',
                },
                parseData: function(res) {
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.total,
                        "data": res.data.rows,
                    };
                }
            }, op));
        },

        upload: function (op = {}) {
            if (!op.url) return false;
            op.url = methods.apiUrl(op.url);
            return upload.render($.extend({
                field: 'file',
                headers: {
                    Authorization: methods.getToken()
                },
            }, op));
        },

        apiUrl: function (url) {
            return AppGeneral.isHttp(url) ? url : setter.request.apiUrl + url;
        },

        getToken: function () {
            const token = AppGeneral.localStorage.getItem('TOKEN');
            return !token ? '' : setter.request.tokenType + token;
        }
    }
    exports("baseMethods", methods)
})
